﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace StudyCode.ORM.Entities
{
    public abstract class Entity
    {
        public abstract long Id { get; set; }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string Serizalite<TEntity>(TEntity entity)
        {
            if(entity==null)
                return null;

            var ser = new DataContractJsonSerializer(typeof (TEntity));
            var ms = new MemoryStream();
            ser.WriteObject(ms, entity);
            var jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
    }
}
